package com.ruoyi.profit.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UnknownFormatConversionException;

import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.mapper.MemberExtInfoMapper;
import com.ruoyi.db.model.param.ProfitInfoParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.AppProfitInfoMapper;
import com.ruoyi.db.domain.AppProfitInfo;
import com.ruoyi.profit.service.IAppProfitInfoService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.domain.entity.SysUser;

import static com.ruoyi.common.utils.ShiroUtils.getSysUser;

/**
 * 项目收益Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-22
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AppProfitInfoServiceImpl implements IAppProfitInfoService 
{
    @Autowired
    private AppProfitInfoMapper appProfitInfoMapper;

    @Autowired
    private MemberExtInfoMapper memberExtInfoMapper;



    /**
     * 查询项目收益
     * 
     * @param id 项目收益主键
     * @return 项目收益
     */
    @Override
    public AppProfitInfo selectAppProfitInfoById(Long id)
    {
        return appProfitInfoMapper.selectAppProfitInfoById(id);
    }

    /**
     * 查询项目收益列表
     * 
     * @param profitInfoParam 项目收益
     * @return 项目收益
     */
    @Override
    public List<AppProfitInfo> selectAppProfitInfoList(ProfitInfoParam profitInfoParam)
    {
        return appProfitInfoMapper.selectAppProfitInfoList(profitInfoParam);
    }

    /**
     * 查询项目收益列表
     *
     * @param profitInfoParam 项目收益
     * @return 项目收益
     */
    @Override
    public List<Map<String, Object>> selectAppProfitInfoListMap(ProfitInfoParam profitInfoParam)
    {
        return appProfitInfoMapper.selectAppProfitInfoListMap(profitInfoParam);
    }

    /**
     * 新增项目收益
     * 
     * @param appProfitInfo 项目收益
     * @return 结果
     */
    @Override
    public int insertAppProfitInfo(AppProfitInfo appProfitInfo)
    {

        appProfitInfo.setMonthInfo(appProfitInfo.getDateInfo().substring(0, 7));
        appProfitInfo.setTotalProfit(NumberUtil.add(appProfitInfo.getDcProfit(), appProfitInfo.getZdProfit()));

        Long aLong = memberExtInfoMapper.countAuthNumber(appProfitInfo.getDateInfo());
        if (BigDecimal.ZERO.compareTo(new BigDecimal(aLong)) != 0){
            appProfitInfo.setZdNumber(aLong.intValue());
            appProfitInfo.setZdAvgProfit(NumberUtil.div(appProfitInfo.getZdProfit(), aLong));
        }

        appProfitInfo.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser = getSysUser();
        appProfitInfo.setCreatorId(sysUser.getUserId().toString());
        appProfitInfo.setCreatorName(sysUser.getUserName().toString());
        return appProfitInfoMapper.insertAppProfitInfo(appProfitInfo);
    }

    /**
     * 修改项目收益
     * 
     * @param appProfitInfo 项目收益
     * @return 结果
     */
    @Override
    public int updateAppProfitInfo(AppProfitInfo appProfitInfo)
    {
        Long aLong = memberExtInfoMapper.countAuthNumber(appProfitInfo.getDateInfo());
        if (BigDecimal.ZERO.compareTo(new BigDecimal(aLong)) != 0){
            appProfitInfo.setZdNumber(aLong.intValue());
            appProfitInfo.setZdAvgProfit(NumberUtil.div(appProfitInfo.getZdProfit(), aLong));
        }
        SysUser sysUser = getSysUser();
        appProfitInfo.setTotalProfit(NumberUtil.add(appProfitInfo.getDcProfit(), appProfitInfo.getZdProfit()));
        appProfitInfo.setUpdatorId(sysUser.getUserId().toString());
        appProfitInfo.setUpdatorName(sysUser.getUserName().toString());
        return appProfitInfoMapper.updateAppProfitInfo(appProfitInfo);
    }

    /**
     * 批量删除项目收益
     * 
     * @param ids 需要删除的项目收益主键
     * @return 结果
     */
    @Override
    public int deleteAppProfitInfoByIds(String ids)
    {
        return appProfitInfoMapper.deleteAppProfitInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目收益信息
     * 
     * @param id 项目收益主键
     * @return 结果
     */
    @Override
    public int deleteAppProfitInfoById(Long id)
    {
        return appProfitInfoMapper.deleteAppProfitInfoById(id);
    }

    @Override
    public List<AppProfitInfo> getIndexShowData() {
        return appProfitInfoMapper.getIndexShowData();
    }

    @Override
    public Map<String, Object> selectAppProfitInfoTotalMap(ProfitInfoParam profitInfoParam) {
        return appProfitInfoMapper.selectAppProfitInfoTotalMap(profitInfoParam);
    }

    @Override
    public List<Map<String, Object>> getIndexMonthShowData() {
        return appProfitInfoMapper.getIndexMonthShowData();
    }
}
